<template>
  <div class="login">
    <div class="login-content">
      <div class="login-form">
        <el-form ref="registerFormRef" :model="activeData" :rules="registerRules">
          <div class="form-title">
            <h2>{{ $t("register.active") }}</h2>
          </div>
          <el-form-item :label="$t('register.customerEmail')" prop="customerEmail">
            <el-input
              ref="customerEmail"
              v-model="activeData.customerEmail"
              name="customerEmail"
              disabled
            />
          </el-form-item>
          <el-form-item :label="$t('register.activeCode')" prop="activeCode">
            <el-input
              ref="activeCode"
              v-model="activeData.activeCode"
              :placeholder="$t('register.activeCode')"
              name="activeCode"
            />
          </el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            class="w-full"
            @click.prevent="handleRegisterActiveSubmit"
          >
            {{ $t("register.submit") }}
          </el-button>
          <router-link :to="{ name: 'Login' }">
            <p style="text-align: center">
              <e-text>{{ $t("register.haveAccount") }},</e-text>
              <el-link type="primary">{{ $t("register.gotoLogin") }}</el-link>
            </p>
          </router-link>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useI18n } from "vue-i18n";
import { useRouter, useRoute } from "vue-router";
import { ref } from "vue";
import type { FormInstance } from "element-plus";
import CustomerAPI, { ActiveForm, Form } from "@/api/customer";

const { t } = useI18n();
const router = useRouter();
const route = useRoute();
const customerEmail = atob(route.params.email as string);

const registerFormRef = ref<FormInstance>();
const loading = ref(false);

const activeData = reactive<ActiveForm>({
  customerEmail: customerEmail,
  activeCode: "",
});

const registerRules = computed(() => {
  return {
    activeCode: [
      {
        required: true,
        message: t("common.plsInput") + t("register.activeCode"),
        trigger: "blur",
      },
    ],
  };
});
// submit to register
async function handleRegisterActiveSubmit() {
  registerFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      CustomerAPI.activeRegister(activeData)
        .then((res) => {
          if (res.data.code === 200) {
            ElMessage.success(res.data.message);
            router.push({ name: "Login" });
            loading.value = false;
          } else {
            ElMessage.error(res.data.message);
          }
        })
        .finally(() => (loading.value = false));
    }
  });
}
</script>

<style lang="scss" scoped>
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  overflow-y: auto;
  background: url("@/assets/images/login-background-light.jpg") no-repeat center right;

  .login-header {
    position: absolute;
    top: 0;
    display: flex;
    justify-content: right;
    width: 100%;
    padding: 15px;

    .logo {
      width: 26px;
      height: 26px;
    }

    .title {
      margin: auto 5px;
      font-size: 24px;
      font-weight: bold;
      color: #3b82f6;
    }
  }

  .login-content {
    display: flex;
    width: 450px;
    overflow: hidden;
    background-color: #fff;
    border-radius: 5px;
    box-shadow: var(--el-box-shadow-light);

    @media (width <= 768px) {
      flex-direction: column;
      max-width: 100%;
      height: 100vh;
      padding: 0 30px;
      border-radius: 0;
      box-shadow: none;
    }

    .login-img {
      display: flex;
      flex: 3;
      align-items: center;
      justify-content: center;
      background: linear-gradient(60deg, #165dff, #6aa1ff);

      @media (width <= 768px) {
        display: none;
      }
    }

    .login-form {
      display: flex;
      flex: 2;
      flex-direction: column;
      justify-content: center;
      min-width: 400px;
      padding: 30px;

      @media (width <= 768px) {
        width: 100%;
        padding: 0 20px;
      }

      .form-title {
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 0 0 20px;
        text-align: center;
      }

      .input-wrapper {
        display: flex;
        align-items: center;
        width: 100%;
      }

      .captcha-img {
        height: 48px;
        cursor: pointer;
        border-top-right-radius: 6px;
        border-bottom-right-radius: 6px;
      }

      .third-party-login {
        display: flex;
        justify-content: center;
        width: 100%;
        color: var(--el-text-color-secondary);

        *:not(:first-child) {
          margin-left: 20px;
        }

        .icon {
          cursor: pointer;
        }
      }
    }
  }

  .login-footer {
    position: absolute;
    bottom: 0;
    width: 100%;
    text-align: center;
  }
}
</style>
